# JavaScript 内置对象

JavaScript 中所有的标准内置对象 (opens new window)、以及它们的方法和属性。

# 一、基础对象

# Object

Object (opens new window) 是存储键值对和复杂实体的基础数据类型。

静态方法

  • Object.fromEntries (opens new window)(iterable)

    将键值对列表(如 Map)转换为对象。

    const entries = new Map([['foo', 'bar'], ['baz', 42]]);
    const obj = Object.fromEntries(entries); // { foo: "bar", baz: 42 }
    
  • Object.defineProperty (opens new window)(obj, prop, descriptor)

    定义或修改对象属性(如不可枚举属性或数据代理)。

    const obj = {car: 'mazda'}
    Object.defineProperty(obj, 'year', { value: 2020 }); // 不可枚举
    
    var number = 18;
    Object.defineProperty(obj, 'age', {  // 数据代理
      get() {
        return number;
      },
    });
    

# Function

所有函数均为 Function (opens new window) 对象实例。

原型方法

  • Function.prototype.call (opens new window)(thisArg, ...args)

    指定 this 值和单独给出的一个或多个参数来调用函数。

    function Product(name, price) {
      this.name = name;
      this.price = price;
    }
    
    function Food(name, price) {
      Product.call(this, name, price); // 继承属性
      this.category = 'food';
    }
    
    console.log(new Food('cheese', 5).name);
    

# WeakMap

WeakMap (opens new window) 对象是一组键值对的集合,其键必须是对象且为弱引用(不会阻止垃圾回收),值可以是任意类型。

原型方法

  • WeakMap.prototype.set (opens new window)(key, value)

    添加键值对。

    const objKey = {};
    weakMap.set(objKey, 'private data');
    
  • WeakMap.prototype.get (opens new window)(key)

    获取键对应的值,无则返回 undefined

    weakMap.get(objKey); // "private data"
    
  • WeakMap.prototype.has (opens new window)(key)

    判断是否存在指定键。

    weakMap.has(objKey); // true
    
  • WeakMap.prototype.delete (opens new window)(key)

    删除指定键值对。

    weakMap.delete(objKey); // true(删除成功)
    

# 二、文本处理对象

# String

String (opens new window) 对象用于表示和操作字符序列。

原型方法

  • String.prototype.replace (opens new window)(pattern, replacement)

    替换匹配内容,支持字符串或正则表达式。

    • 返回一个新字符串

    • pattern 可以是字符串或 RegExpreplacement 可以是字符串或一个在每次匹配时调用的函数

    • 如果 pattern 是字符串,则仅替换第一个匹配项

    '你的唯一'.replace('你', '我'); // "我的唯一"
    
    // Camel case 转 Snake case
    const camelToSnakeCase = str => str.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`);
    
  • String.prototype.slice (opens new window)(indexStart, indexEnd)

    提取子字符串(含头不含尾)。

    • 返回一个新字符串

    • 如果 indexEnd 被省略,则 slice() 提取到字符串的末尾

    • 如果 indexStart 为负数,则索引从字符串末尾开始计数,即从 str.length + indexStart 开始

    'JavaScript'.slice(0, 4); // "Java"
    
  • String.prototype.charAt (opens new window)(index)

    提取指定索引处的单个 UTF-16 码元构成的新字符串。

    const sentence = 'The quick brown fox jumps over the lazy dog.';
    const index = 4;
    console.log(`The character at index ${index} is ${sentence.charAt(index)}`);
    
  • String.prototype.charCodeAt (opens new window)(index)

    返回给定索引处的 UTF-16 代码单元,其值介于 065535 之间。

    const sentence = 'The quick brown fox jumps over the lazy dog.';
    const index = 4;
    console.log(
      `Character code ${sentence.charCodeAt(index)} is equal to ${sentence.charAt(index)}`,
    );
    
  • String.prototype.codePointAt (opens new window)(index)

    返回一个 Unicode 编码点值的非负整数。

    const icons = '😄';
    console.log(icons.codePointAt(0)); // Expected output: "128516"
    

# 三、索引集合对象

# Array

Array (opens new window) 对象是存储有序元素的集合。

静态方法

原型方法

  • Array.prototype.join (opens new window)(separator)

    将数组元素拼接为字符串,用逗号或指定的分隔符字符串分隔。

    [1, 2, 3].join(' - '); // "1 - 2 - 3"
    
    $("input[name='checkList']:checked")
      .map(function() {
        return $(this).val();
      })
      .get()
      .join();
    
  • Array.prototype.slice (opens new window)(start, end)

    返回新的数组对象,由 [start, end) 决定的原数组的浅拷贝。

    const array = [1, 2, 3, 4, 5];
    const slice = Array.prototype.slice.call(array, 1, 3);
    
  • Array.prototype.filter (opens new window)(callbackFn, thisArg)

    过滤满足条件的元素。

    [1, 2, 3].filter(num => num > 1); // [2, 3]
    
    const words = ['spray', 'elite', 'exuberant', 'destruction', 'present'];
    const result = words.filter(word => word.length > 6);
    

# TypedArray

TypedArray (opens new window) 对象描述了底层二进制数据缓冲区的类数组视图。没有称为 TypedArray (opens new window) 的全局属性,也没有直接可用的 TypedArray 构造函数。

# Uint8Array

Uint8Array (opens new window) 数组类型表示一个 8 位无符号整数数组(值范围 0 - 255)。

构造函数

使用 Uint8Array() (opens new window) 构造函数创建 Uint8Array (opens new window) 对象。除非明确提供初始化数据,否则内容将初始化为 0

const uint8 = new Uint8Array(5);
console.log(uint8);                 // 输出: Uint8Array(5) [ 0, 0, 0, 0, 0 ]

uint8[0] = 10;
uint8[1] = 20;
uint8[2] = 30;
uint8[3] = 255;
uint8[4] = 256;                     // 超出范围的值会被模 256 操作 (256 % 256 = 0)
console.log(uint8);                 // 输出: Uint8Array(5) [ 10, 20, 30, 255, 0 ]

通过其他数组或值初始化 Uint8Array

const uint8 = new Uint8Array([255, 256]); 
console.log(uint8);                 // [255, 0](256 溢出为 0)

Uint8Array (opens new window) 可以与 ArrayBuffer (opens new window) 一起使用,用于更灵活地操作二进制数据。

const buffer = new ArrayBuffer(8);
const uint8FromBuffer = new Uint8Array(buffer);

uint8FromBuffer[0] = 100;
uint8FromBuffer[1] = 200;

console.log(uint8FromBuffer);        // 输出: Uint8Array(8) [ 100, 200, 0, 0, 0, 0, 0, 0 ]

// 查看底层的 ArrayBuffer
console.log(uint8FromBuffer.buffer); // 输出: ArrayBuffer(8)

# 四、结构化数据对象

# ArrayBuffer

ArrayBuffer (opens new window) 对象表示通用的、固定长度的原始二进制数据缓冲区。它是一个字节数组,通常在其他语言中称为 byte array

不能直接操作 ArrayBuffer 中的内容,而是要通过 TypedArrayDataView 对象来操作,它们会将缓冲区中的数据表示为特定的格式,并通过这些格式来读写缓冲区的内容。

const buffer = new ArrayBuffer(8);
const uint8View = new Uint8Array(buffer);
uint8View[0] = 255; // 操作二进制数据

# 五、数字与日期对象

用来表示数字、日期和执行数学计算。

# Date

Date (opens new window) 对象表示日期和时间。

构造函数

当没有提供参数时,新创建的 Date (opens new window) 对象代表当前的日期和时间。

new Date().toLocaleString();

原型方法

# 六、反射对象

# Proxy

Proxy (opens new window) 对象用于创建对象代理以拦截操作。

构造函数

使用 Proxy() (opens new window) 构造函数创建 Proxy (opens new window) 对象。

// 当对象中不存在对应属性名时,默认返回值为 37
const handler = { get: (obj, prop) => prop in obj ? obj[prop] : 37 };
const p = new Proxy({}, handler);
console.log(p.a); // 37(默认值)

# Reflect

Reflect (opens new window) 对象提供拦截 JavaScript 操作的方法。

静态方法

  • Reflect.get (opens new window)(target, propertyKey[, receiver])

    获取对象属性值。

    Reflect.get({ x: 1 }, 'x'); // 1